package com.yangoxix.lovegoing.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yangoxix.lovegoing.mapper.ActiticyMapper;
import com.yangoxix.lovegoing.mapper.UserInActivityMapper;
import com.yangoxix.lovegoing.pojo.Activity;
import com.yangoxix.lovegoing.pojo.UserInActivity;
import com.yangoxix.lovegoing.util.timeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * The type Sign service.
 *
 * @program: lovegoing
 * @description: 签到接口的服务层
 * @author: chenzou
 * @create: 2021 -09-23 09:15
 */
@Service
public class SignService {
    @Autowired
    private ActiticyMapper mapper;
    @Autowired
    private UserInActivityMapper uaMapper;

    /**
     * 判断是否已经开始签到
     *
     * @param activityname the activityname
     * @return the boolean
     */
    public String isSign(String activityname) {
        Activity activity = new Activity();
        Activity find = activity.selectById(activityname);
        String code = find.getSigncode();
        return code;
    }

    /**
     * Begin sign boolean.开始签到活动
     *
     * @param hours the hours。截止小时数
     * @param title the title，活动名称
     * @return string string
     */
    public String beginSign(int hours, String title) {

        Instant afterhour = timeUtil.getAfterhour(hours);
        java.util.Date from = Date.from(afterhour);
        UpdateWrapper<Activity> wrapper = new UpdateWrapper<>();
        String code = getSignCode(6);
        wrapper.eq("activityname", title).set("startsign", from).set("signcode", code);
        int update = mapper.update(null, wrapper);
        return code;
    }

    /**
     * Is over time boolean.判断是否超时
     *
     * @param title the title
     * @return the boolean
     */
    public boolean isOverTime(String title) {
        Activity activity = new Activity();
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("activityname", title);
        Activity find = activity.selectOne(wrapper);
        Date time = find.getStartsign();
        Instant currentTime = timeUtil.getCurrentTime();
        Instant instant = time.toInstant();
        int i = currentTime.compareTo(instant);
        return i < 0;
    }

    /**
     * Is applyed boolean.判断是否有参加活动
     *
     * @param titleString the title string
     * @param userName    the user name
     * @return the boolean false:错误 true:正确
     */
    public boolean isApplyed(String titleString, String userName) {
        UserInActivity userInActivity = new UserInActivity();
        userInActivity.setActivityname(titleString);
        userInActivity.setUsername(userName);
        UserInActivity one = userInActivity.selectOne(null);
        if (one == null) {
            return false;
        }
        return true;
    }

    /**
     * To sign.对活动进行签到
     *
     * @param username     the username
     * @param activityname the activityname
     */
    public void toSign(String username, String activityname) {
        UpdateWrapper<UserInActivity> wra = new UpdateWrapper<UserInActivity>()
                .eq("username", username)
                .eq("activityname", activityname)
                .set("sign", 2);
        uaMapper.update(null, wra);
    }

    /**
     * Get sign code int.生成随机的签到码
     *
     * @param digit the digit 随机数的位数
     * @return the int
     */
    public String getSignCode(int digit) {
        double pow = Math.pow(10, digit-1);
        int code = (int) ((Math.random() * 9 + 1) * pow);
        return Integer.toString(code);
    }

    /**
     * Get sign user list.获取该活动已签到人员
     *
     * @param activityname the activityname
     * @return the list
     */
    public List<String> getSignUser(String activityname){
        UserInActivity userInActivity = new UserInActivity();
        QueryWrapper<UserInActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("activityname", activityname).eq("sign",2);
        List<UserInActivity> uas = userInActivity.selectList(wrapper);
        List<String> names = uas.stream().map(UserInActivity::getUsername).collect(Collectors.toList());
        return names;
    }

    /**
     * Checkcode string.检查签到码
     *
     * @param activityname the activityname
     * @param code         the code
     * @return the string
     */
    public String checkcode(String activityname,String code) {
        Activity activity = new Activity();
        Activity find = activity.selectById(activityname);
        if (find == null) {
            //can't find
            return "找不到";
        }else{
            //find it
            if (find.getSigncode().equals(code)) {
                //correct
                return "correct";

            }else{
                //incorrect
                return "签到码不正确";

            }
        }

    }

    /**
     * Check is signed string.检查是否签到
     *
     * @return the string
     */
    public String checkIsSigned(String username,String activityname){
        UserInActivity userinActivity = new UserInActivity();
        QueryWrapper<UserInActivity> wrapper = new QueryWrapper<UserInActivity>().eq("username",username).eq("activityname",activityname);
        UserInActivity find = userinActivity.selectOne(wrapper);
        int sign = find.getSign();
        if (sign == 2) {
            return "已经签到过了";
        }
        return "success";
    }
}
